<script setup>
import PageContainer from '@/components/PageContainer.vue';
import ChannelSelect from '@/components/ChannelSelect.vue';
import { Delete, Edit } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { artGetListService, artDelService } from '@/api/article'
import { formatTime } from '@/utils/format';
import ArticleEdit from '@/components/ArticleEdit.vue';
import { ElMessage, ElMessageBox } from 'element-plus';

const value = ref('')

const params = ref({
  pagenum: 1,
  pagesize: 2,
  cate_id: '',
  state: ''
})
const loading = ref(false)
// 获取文章列表
const articleList = ref([])
const total = ref(0)
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getArticleList()

// 分页逻辑
const onSizeChange = (size) => {
  params.value.pagenum = 1
  params.value.pagesize = size
  getArticleList()
}
const onCurrentChange = (page) => {
  params.value.pagenum = page
  getArticleList()
}

// 搜索和重置
const onSearch = () => {
  params.value.pagenum = 1
  getArticleList()
}

const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}

// 添加和编辑
const articleEditRef = ref()
const onAddArticle = () => {
  articleEditRef.value.open({})
}
const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}
const onSuccess = (state) => {
  if (state === 'add') {
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getArticleList()
}

// 删除
const onDelArticle = async (row) => {
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  getArticleList()
}
</script>

<template>
  <PageContainer title="文章管理">
    <template #btn>
      <el-button type="primary" @click="onAddArticle">发布文章</el-button>
      <ArticleEdit ref="articleEditRef" @success="onSuccess"></ArticleEdit>
    </template>
    <template #main>
      <el-form inline>
        <el-form-item label="文章分类: ">
          <!-- vue2 v-model  :value和 @input -->
          <!-- vue3 v-model :modelValue 和 @update:modelValue -->
          <channel-select width="150px" v-model="params.cate_id"></channel-select>
        </el-form-item>
        <el-form-item label="发布状态: ">
          <el-select v-model="params.state" style="width: 150px;">
            <el-option label="已发布" value="已发布"></el-option>
            <el-option label="草稿" value="草稿"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">搜索</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="articleList" style="width: 100%;" v-loading="loading">
        <el-table-column label="文章标题" width="400">
          <template #default="scope">
            <el-link type="primary" :underline="false">{{ scope.row.title }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="分类" prop="cate_name"></el-table-column>
        <el-table-column label="发表时间" prop="pub_date">
          <template #default="{ row }">
            {{ formatTime(row.pub_data) }}
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="state"></el-table-column>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button circle plain :icon="Edit" @click="onEditArticle(scope.row)">
            </el-button>
            <el-button circle plain :icon="Delete" type="danger" @click="onDelArticle(scope.row)">
            </el-button>
          </template>
        </el-table-column>
        <template #empty>
          <el-empty description="没有数据"></el-empty>
        </template>
      </el-table>
      <el-pagination v-model:current-page="params.pagenum" v-model:page-size="params.pagesize"
        :page-sizes="[2, 3, 4, 5, 10]" layout="jumper, total, sizes, prev, pager, next" background :total="total"
        @size-change="onSizeChange" @current-change="onCurrentChange"
        style="margin-top: 20px; justify-content: flex-end" />
    </template>
  </PageContainer>

</template>
<style scoped lang="scss"></style>
